﻿using Newtonsoft.Json;
using ShortcutMenu.Manager;
using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ShortcutMenu.Render
{
    public class ApplyViewRender : RenderBase
    {
        private Canvas _rootElement;

        private List<ApplyData> _applyDatas;
        public List<ApplyData> ApplyDatas
        {
            get
            {
                if (_applyDatas == null) { _applyDatas = MenuApplication.GetManager<DispositionManager>().DispositionFile.ApplyDatas; }
                return _applyDatas;
            }
            set { _applyDatas = value; }
        }

        private List<ApplyElement> _applyElements = new List<ApplyElement>();

        /// <summary>
        /// 显示矩阵[会在ApplyView_SizeChanged事件中进行赋值]
        /// </summary>
        private int[][] _generateMatrixArr;
        public int[][] GenerateMatrixArr
        {
            get { return _generateMatrixArr; }
            set { _generateMatrixArr = value; }
        }


        public ApplyViewRender(MainWindow mainWindow) : base(mainWindow)
        {
            _rootElement = mainWindow.ApplyView;

        }

        /// <summary>
        /// 在获取到宽高时执行
        /// </summary>
        public override void Show()
        {
            _rootElement.Children.Clear();
            foreach (var item in ApplyDatas)
            {
                var element = ElementFactoryManage.Create<ApplyElement, ApplyData>(item);
                if (SetPoint(element, ApplyFactory.CurrX, ApplyFactory.CurrY))
                {
                    _applyElements.Add(element);
                    _rootElement.Children.Add(element.RootElement);
                }
                else
                {
                    Log.Write($"计算位置失败,JsonData:{JsonConvert.SerializeObject(element.Data)}");
                }
            }
            SetRootElementHeight();
        }

        public override void Hide()
        {

        }

        #region 生成方格
        /// <summary>
        /// 更改方格位置
        /// </summary>
        /// <param name="apply">指定元素</param>
        /// <param name="currX">索引指针X</param>
        /// <param name="CurrY">索引指针Y</param>
        /// <returns>true:更改成功,false更改失败</returns>
        private bool SetPoint(ApplyElement apply, int currX, int CurrY)
        {
            var arr = GenerateMatrixArr;
            System.DrawingCore.Point point = GenerateMatrixOrder.GetPoint(ref arr, currX, CurrY, apply.Stations);
            if (point.X == -1 || point.Y == -1)
            {
                //失败
                return false;
            }
            //指针更新
            ApplyFactory.CurrX = point.X;
            ApplyFactory.CurrY = point.Y;
            //数据更新
            apply.Data.X = point.X;
            apply.Data.Y = point.Y;

            //进行位置复写
            GenerateMatrixOrder.Set(ref arr, point, apply.Stations);
            //重整位置
            apply.Margin = new Thickness((point.X * (ApplyElement._sideLength + ApplyElement._margin)) + ApplyElement._margin, (point.Y * (ApplyElement._sideLength + ApplyElement._margin)) + ApplyElement._margin, 0, 0);

            GenerateMatrixArr = arr;

            return true;
        }
        #endregion

        /// <summary>
        /// 创建单项
        /// </summary>
        public void CreateItem(ApplyData data)
        {
            var element = ElementFactoryManage.Create<ApplyElement, ApplyData>(data);
            if (SetPoint(element, ApplyFactory.CurrX, ApplyFactory.CurrY))//这个地方从头开始遍历[不行,会在下一次全刷的时候放到最后一位]
            {
                ApplyDatas.Add(data);
                _applyElements.Add(element);
                _rootElement.Children.Add(element.RootElement);
            }
            else
            {
                Log.Write($"计算位置失败,JsonData:{JsonConvert.SerializeObject(element.Data)}");
            }
            SetRootElementHeight();
        }

        /// <summary>
        /// 更改根元素高度
        /// </summary>
        private void SetRootElementHeight()
        {
            _rootElement.Height = GenerateMatrixArr.Length * (ApplyElement._margin + ApplyElement._sideLength);
        }
    }
}
